﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DialControlLibrary
{
    /// <summary>
    /// MercuryThermometer.xaml 的交互逻辑
    /// </summary>
    public partial class MercuryThermometer : UserControl
    {
        public MercuryThermometer()
        {
            InitializeComponent();
            this.TickMarkRepaint();
        }


        public int Minimum
        {
            get { return (int)GetValue(MininumProperty); }
            set { SetValue(MininumProperty, value); }
        }

        public static readonly DependencyProperty MininumProperty =
            DependencyProperty.Register("Minimum", typeof(int), typeof(MercuryThermometer), new PropertyMetadata(-20, new PropertyChangedCallback(ValueChanged)));




        public int Maximum
        {
            get { return (int)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.Register("Maximum", typeof(int), typeof(MercuryThermometer), new PropertyMetadata(50, new PropertyChangedCallback(ValueChanged)));



        public int DisplayValue
        {
            get { return (int)GetValue(DisplayValueProperty); }
            set { SetValue(DisplayValueProperty, value); }
        }

        public static readonly DependencyProperty DisplayValueProperty =
            DependencyProperty.Register("DisplayValue", typeof(int), typeof(MercuryThermometer), new PropertyMetadata(0, new PropertyChangedCallback(ValueChanged)));


        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as MercuryThermometer)?.TickMarkRepaint();
        }
        private void TickMarkRepaint()
        {
            this.TickBorder.Children.Clear();
            //水银柱高度
            double TickBorderHeight = 121;
            //刻度线数量
            double RangeInterval = (Maximum - Minimum) / 2;
            //刻度间间距
            double GraduationInterval = TickBorderHeight / RangeInterval;
            for (int i = 0; i <= RangeInterval; i++)
            {

                Line line = new Line();
                line.X1 = 2;
                line.Y1 = i * GraduationInterval;
                line.X2 = 10;
                line.Y2 = i * GraduationInterval;

                Color lineColor = (Color)ColorConverter.ConvertFromString("#BCA258");
                Brush lineBrush = new SolidColorBrush(lineColor);
                line.Stroke = lineBrush;
                line.StrokeThickness = 1;

                if (i % 5 == 0)
                {
                    line.X2 = 10;
                    line.Y2 = i * GraduationInterval;

                    TextBlock textBlock = new TextBlock()
                    {
                        FontSize = 10,
                        Width = 20,
                        FontFamily = new FontFamily("Arial"),
                        TextAlignment = TextAlignment.Right,
                        Foreground = lineBrush,
                        Text = (Maximum - i * 2).ToString(),
                        Margin = new Thickness(6, -5 + i * GraduationInterval, 0, 0),
                    };
                    this.TickBorder.Children.Add(textBlock);
                }
                else
                {
                    line.X2 = 4.5;
                }
                this.TickBorder.Children.Add(line);
            }
            var disValue = this.DisplayValue;
            if (this.DisplayValue < this.Minimum)
                disValue = this.Minimum;
            if (this.DisplayValue > this.Maximum)
                disValue = this.Maximum;
            var transHeight = (disValue - this.Minimum) * (GraduationInterval / 2) + 10;

            DoubleAnimation doubleAnimation = new DoubleAnimation(transHeight, TimeSpan.FromMilliseconds(500));
            this.DisplayHeightBorder.BeginAnimation(HeightProperty, doubleAnimation);
        }
    }
}
